package com.qf.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.pojo.User;
import org.springframework.web.bind.annotation.*;

/**
 * @author 千锋健哥
 */
@RestController
@RequestMapping("/search")
public class SearchController {

    @RequestMapping("/hello")
    public String hello() {
        return "搜索微服务已经被调用到, 正常返回结果!";
    }

    @RequestMapping("/findById")
    @HystrixCommand(fallbackMethod = "findUserByIdFallBack")
    public User findUserById(@RequestParam("id") Integer id) {
        //int i = 1/0;
        User user = new User();
        user.setId(1);
        user.setName("青龙");
        return user;
    }

    /**
     * hystrix的兜底方法
     * @param id
     * @return
     */
    public User findUserByIdFallBack(Integer id) {
        User user = new User();
        user.setId(0);
        user.setName("调用方发生了异常, hystrix兜底");
        return user;
    }

    /**
     * 传参   http://localhost:9002/search/inserUser?id=1&name=xxx
     * @param user
     * @return
     */
    @RequestMapping("/insertUser")
    public String insertUser(@RequestBody User user) {
        System.out.println("======" + user);
        return "添加成功!";
    }
}
